package com.juli.school.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.juli.school.dao.entity.Cid;
import com.juli.school.dao.entity.Dept;
import com.juli.school.dao.mapper.CidMapper;
import com.juli.school.dao.mapper.DeptMapper;
import com.juli.school.service.bo.CidBo;
import com.juli.school.service.bo.DeptBo;
import com.juli.school.service.utils.BeanUtils;
import com.juli.school.utils.core.model.CommonResp;

/**
 * 基站优惠
 * 
 * @author wjp
 * @date 2017年9月11日
 *
 */
@Service
public class CidService {
	
	private static final Logger logger = LoggerFactory.getLogger(CidService.class);
	
	@Resource
	private CidMapper cidMapper;
	
	@Resource
	private DeptMapper deptMapper;
	
	public CommonResp<CidBo> detail(String cid_id) {
		CommonResp<CidBo> commonResp = new CommonResp<CidBo>();
		try {
			if (Strings.isNullOrEmpty(cid_id)) {
				commonResp.fail("入参不能为空");
				return commonResp;
			}
			CidBo cidBo = new CidBo();
			cidBo.setCidId(Integer.valueOf(cid_id));
			Cid cid = cidMapper.get(cidBo);
			if (cid == null) {
				commonResp.exception("没有获取到信息");
				return commonResp;
			}
			BeanUtils.copyProperties(cidBo, cid);
			if (cid.getCollId() != null) {
				DeptBo deptBo = new DeptBo();
				deptBo.setCollId(cid.getCollId());
				List<Dept> list = deptMapper.list(deptBo);
				cidBo.setDeptList(list);
			}
			commonResp.setData(cidBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("获取基站优惠详情异常", e);
			commonResp.exception("获取基站优惠详情异常");
		}
		return commonResp;
	}
	
	public CommonResp<List<CidBo>> list(Cid cid) {
		CommonResp<List<CidBo>> commonResp = new CommonResp<List<CidBo>>();
		try {
			if ("".equals(cid.getMnc() == null ? "" : cid.getMnc()) || "".equals(cid.getCidId() == null ? "" : cid.getCidId())) {
				commonResp.fail("入参必须包含mnc和cidId");
				return commonResp;
			}
			List<Cid> listPage = cidMapper.list(cid);
			List<CidBo> data = Lists.newArrayList();
			for (Cid findCid : listPage) {
				CidBo cb = new CidBo();
				BeanUtils.copyProperties(cb, findCid);
				if (findCid.getCollId() != null) {
					DeptBo deptBo = new DeptBo();
					deptBo.setCollId(findCid.getCollId());
					List<Dept> list = deptMapper.list(deptBo);
					cb.setDeptList(list);
				}
				data.add(cb);
			}
			commonResp.success();
			commonResp.setData(data);
		} catch (BeansException e) {
			logger.error("获取基站列表发生异常", e);
			commonResp.exception("获取基站列表发生异常");
		}
		return commonResp;
	}
}
